<?php
namespace app\api\controller;

use app\common\controller\Base;
use app\common\controller\Common;
use app\common\exception\FailException;
use app\common\exception\ParameterException;
use app\common\logic\Oss;
use think\Db;
use think\Request;
use app\common\logic\Praise as PraiseLogic;
use think\Loader;


/**
 * @title 评价模块接口
 * @description 接口说明
 * @group 接口分组
 * @header name:token require:1 default: desc:token
 * @param name:user_type type:int require:1 default:1 other: desc:状态(1.会员登陆，2.商家，3.代理商，4.省级代理商，5.内部员工)
 *
 */

class Praise extends Base
{
    /**
     * @title 订单评价
     * @description 接口说明
     * @author WCJ
     * @url /api/praise/add_praise
     * @method POST
     * @param name:order_id type:int require:0 default:1 other: desc:订单id
     * @param name:pic_id type:int require:0 default:1 other: desc:图片id[1,2,3,4]
     * @param name:content type:int require:0 default:1 other: desc:评论内容
     * @param name:star type:int require:0 default:1 other: desc:星星
     * @param name:praise_picture type:int require:0 default:1 other: desc:图片
     *
     * @return data:数据
     *
     */
    public function add_praise(Request $request)
    {
        $uid =$this->uid;
        $order_id = $request->post("order_id");
        $content = $request->post("content");
        $star=$request->post("star")?$request->post("star"):0;
        $praise_picture = $request->post("praise_picture");

        $validate = Loader::validate("Common");
        if($star){
            if (!$validate->scene("common")->check(['id' => $star])) {
                throw new ParameterException(['code' => 201,'errorCode' => 404, 'msg' => $validate->getError()]);
            }
        }


        if (!$validate->scene("shopid")->check(['shopid' => $order_id])) {
            throw new ParameterException(['code' => 201,'errorCode' => 404, 'msg' => $validate->getError()]);
        }



        $praisedata=new PraiseLogic();
        $data= $praisedata->add_praise($order_id,$uid,$content,$star,$praise_picture);
        $datas= ["code"=>200,"msg"=>"评价成功","data"=>$data];
        if($this->new_token){
            $datas["new_token"]=$this->new_token;
        }
     
        return $datas;
    }

//    /**
//     * @title 评价上传图片
//     * @description 接口说明
//     * @author WCJ
//     * @url /api/praise/praise_upload
//     * @method POST
//     * @param name:praise_picture type:file require:0 default:1 other: desc:上传图片
//     *
//     * @return shop_count:数量
//     *
//     */
//    public function praise_upload(Request $request)
//    {
//        $uid =$this->uid;
//        $praise_picture = $request->file("praise_picture");
//
//// 判断图片有效性
//        if (!$praise_picture->isValid()) {
//            throw new ParameterException(['code' => 201,"msg"=>"无效图片","errorCode"=>404]);
//        }
//// 获取图片在临时文件中的地址
//        $pic = $praise_picture->getRealPath();
//// 制作文件名
//        $key = time() . rand(10000, 99999999) . '.jpg';
////阿里 OSS 图片上传
//        $result = Oss::publicUpload("zdtx023",$key, $pic);
//        if ($result) {
//            // success
//            $pcid=Db::table("t_picture")->insertGetId([
//                "picurl"=>$key,
//                "fileName"=>$key,
//                "addtime"=>time(),
//                "updated_at"=>date("Y-m-d H:i:s",time()),
//                "created_at"=>date("Y-m-d H:i:s",time()),
//                "by_uid"=>$uid
//            ]);
//            $datas= ["code"=>200,"data"=>$pcid,"msg"=>"成功","url"=>'https://zdtx023.oss-cn-shanghai.aliyuncs.com/'.$key];
//            if($this->new_token){
//                $datas["new_token"]=$this->new_token;
//            }
//            return $datas;
//
//        } else {
//            // fail
//            throw new FailException(['code' => 201,"msg"=>"上传失败","errorCode"=>404]);
//        }
//    }

    /**
     * @title 用户评价列表
     * @description 接口说明
     * @author WCJ
     * @url /api/praise/user_praise
     * @method POST
     * @param name:start type:int require:0 default:0 other: desc:开始数
     * @param name:num type:int require:0 default:20 other: desc:个数
     *
     * @return data:数据
     *
     */
    public function user_praise(Request $request)
    {
        $uid =$this->uid;
        $start = $request->post('start') ? $request->post('start') : 1;
        $num = $request->post('num') ? $request->post('num') : 20;

        $praisedata=new PraiseLogic();
        $data=$praisedata->get_user_praise($uid,$start,$num);
        $datas= ["code"=>200,"msg"=>"成功","data"=>$data];

        if($this->new_token){
            $datas["new_token"]=$this->new_token;
        }
        return $datas;
    }


//    /**
//     * @title 用户评价测试
//     * @description 接口说明
//     * @author WCJ
//     * @url /api/praise/user_praise_test
//     * @method POST
//     * @param name:praise_picture type:int require:0 default:0 other: desc:图组
//     *
//     * @return data:数据
//     *
//     */
//    public function user_praise_test(Request $request)
//    {
//        // $uid =$this->uid;
//        $praise_picture = json_decode($request->post("praise_picture"),true);
//        //$up_dir = ROOT_PATH . 'public' . DS .  'uploads/';
//        foreach ($praise_picture as $picture){
//
//            if(preg_match('/^(data:\s*image\/(\w+);base64,)/', $picture, $result)){
//                $type = $result[2];
//                $key = time() . rand(10000, 99999999).'.'.$type;
//                $new_file = $key;
//                if(in_array($type,array('pjpeg','jpeg','jpg','gif','bmp','png'))){
//                    if(file_put_contents($new_file, base64_decode(str_replace($result[1], ' ', $picture)))){
//                        $results = Oss::publicUpload("zdtx023",'pingjia/'.$key, $new_file);
//                    }
//
//                }
//            }
//        }
//
//        if($results){
//            return ["code"=>200,"msg"=>"成功"];
//        }else{
//            return ["code"=>404,"msg"=>"错误"];
//        }
//
//    }






}